#region Using directives
using System;
using System.Collections.Generic;
using SharpWind.Helpers;
#endregion

namespace SharpWind.Resources
{
	public abstract class ResourceManager<T> : IDisposable where T : Resource, new()
	{
		#region Fields

		protected Dictionary<string, T> resources = new Dictionary<string, T>();

		protected Log log = null;

		protected Settings settings = null;

		#endregion

		#region Constructor

		public ResourceManager()
		{
			Engine.Instance.Garbage.Add( this );
		}

		#endregion

		#region Methods

		public abstract bool Initialize( Log log, Settings settings );

		public T Load( string name )
		{
			if( string.IsNullOrEmpty( name )  )
			{
				return null;
			}
			else
			{
				if( this.resources.ContainsKey( name ) )
				{
					return this.resources[name];
				}

				T resource = new T();
				if( resource.Load( name ) )
				{
					this.resources.Add( name, resource );
					
					return resource;
				}
			}

			return null;
		}

		public void Dispose()
		{
			foreach( T resource in this.resources.Values )
			{
				resource.Dispose();
			}

			this.resources.Clear();

			Dispose( true );
			GC.SuppressFinalize( this );
		}

		protected virtual void Dispose( bool disposing )
		{
			
		}

		#endregion

		#region Properties

		public T this[string name]
		{
			get
			{
				if( !string.IsNullOrEmpty( name ) && this.resources.ContainsKey( name ) )
				{
					return this.resources[name];
				}
				else
				{
					return null;
				}
			}
		}

		public int Count
		{
			get
			{
				return this.resources.Count;
			}
		}

		#endregion
	}
}
